package com.hdoit.ares.logical.official.dao.impl;

import java.util.List;

import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import com.hdoit.ares.logical.official.dao.IArticalClassifyDao;
import com.hdoit.ares.logical.official.model.ArticalClassify;
import com.hdoit.ares.logical.viewmodel.official.ArticalClassifySearch;
import com.hdoit.ares.logical.viewmodel.official.ArticalClassifyTree;
import com.tennetcn.common.dao.base.IFinderExpression;
import com.tennetcn.common.dao.base.impl.SuperDao;
import com.tennetcn.common.enums.OrderEnum;
import com.tennetcn.common.enums.pub.YesOrNo;
import com.tennetcn.common.utils.FinderExpressionFactory;
/**
 * @author      yujinhui
 * @email       
 * @createtime  2018年01月08日 17:10:44
 * @comment
 */
@Component
public class ArticalClassifyDaoImpl extends SuperDao<ArticalClassify> implements IArticalClassifyDao{

	@Override
	public List<ArticalClassifyTree> queryListMenuTree(ArticalClassifySearch search) {
		IFinderExpression finderExpression=FinderExpressionFactory.createExpression();
		
		finderExpression.addBody("select * from ",ArticalClassify.class)
						.andWhere("deleteMark = #{deleteMark}")
						.setParam("deleteMark", YesOrNo.no)
						.addOrder("sortCode", OrderEnum.asc);
		
		appendFinder(finderExpression, search);
		
		return queryList(finderExpression,search,ArticalClassifyTree.class);
	}

	private void appendFinder(IFinderExpression finderExpression, ArticalClassifySearch search) {
		if(search.getHidden()>-1){
			finderExpression.andWhere("hidden=#{hidden}")
							.setParam("hidden", search.getHidden());
		}
		
		if(!StringUtils.isEmpty(search.getUseType())){
			finderExpression.andWhere("useType=#{useType}")
							.setParam("useType", search.getUseType());
		}
		
		if(!StringUtils.isEmpty(search.getButtonType())){
			finderExpression.andWhere("buttonType=#{buttonType}")
							.setParam("buttonType", search.getButtonType());
		}
		
		if(!StringUtils.isEmpty(search.getType())){
			finderExpression.andWhere("type=#{type}")
							.setParam("type", search.getType());
		}
		
		if(!StringUtils.isEmpty(search.getFunctionURL())){
			finderExpression.andWhere("functionURL = #{functionURL}")
				  			.setParam("functionURL", search.getFunctionURL());
		}
		
		
		if(!StringUtils.isEmpty(search.getParentId())){
			finderExpression.andWhere(finderExpression.getMainTableAlias()+"parentId=#{parentId}")
							.setParam("parentId", search.getParentId());
		}
		
		if(!StringUtils.isEmpty(search.getMenuMark())){
			finderExpression.andWhere("menuMark=#{menuMark}")
							.setParam("menuMark", search.getMenuMark());
		}
		
		
		if(search.getTypes()!=null&&search.getTypes().size()>0){
			String typeWheres="";
			for (int i=0;i<search.getTypes().size();i++) {
				if(StringUtils.isEmpty(typeWheres)){
					typeWheres+=" type=#{type"+i+"}";
				}else{
					typeWheres+=" or type=#{type"+i+"}";
				}
				finderExpression.setParam("type"+i, search.getTypes().get(i));
			}
			finderExpression.andWhere(typeWheres);
		}
		
		
		/*return finderExpression;*/
		
	}

	@Override
	public List<ArticalClassify> queryList(ArticalClassifySearch search) {
		IFinderExpression finderExpression=FinderExpressionFactory.createExpression();
		
		finderExpression.addBody("select * from ",ArticalClassify.class)
						.addOrder("sortCode", OrderEnum.asc);
		
		appendFinder(finderExpression, search);
		
		return queryList(finderExpression,search);
	}

	/*根据类型和父级Id查询所有列表*/
	@Override
	public List<ArticalClassify> queryOnlineSerList(String type, String parentId) {
        
		IFinderExpression finderExpression=FinderExpressionFactory.createExpression();
		
		finderExpression.addBody("select * from ",ArticalClassify.class)
						.andWhere("deleteMark = #{deleteMark}")
						.setParam("deleteMark", YesOrNo.no)
						.andWhere("type = #{type}")
						.setParam("type", type)
						.andWhere("parentId = #{parentId}")
						.setParam("parentId", parentId)
						.addOrder("sortCode", OrderEnum.asc);
		
		return queryList(finderExpression);
	}

	@Override
	public ArticalClassify queryModel(ArticalClassifySearch search) {
		IFinderExpression finderExpression=FinderExpressionFactory.createExpression();
		
		finderExpression.addBody("select * from ",ArticalClassify.class)
						.andWhere("deleteMark = #{deleteMark}")
						.setParam("deleteMark", YesOrNo.no)
						.addOrder("sortCode", OrderEnum.asc);
		
		appendFinder(finderExpression, search);
		
		return queryModel(finderExpression);
	}


}